package com.tanhua.management.controller;

import cn.hutool.core.util.ObjectUtil;
import com.tanhua.management.service.ManageService;
import com.tanhua.management.vo.PageResult;
import com.tanhua.management.vo.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/manage")
@Slf4j
public class ManageController {

    @Autowired
    private ManageService manageService;

    @GetMapping("/users")
    public ResponseEntity<PageResult> queryUsers(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                 @RequestParam(value = "pagesize", defaultValue = "1") Integer pageSize,
                                                 @RequestParam(value = "id", required = false) Long userId,
                                                 @RequestParam(value = "nickname", required = false) String nickName,
                                                 @RequestParam(value = "city", required = false) String city) {

        try {
            PageResult pageResult = manageService.queryUsers(userId, nickName, city, page, pageSize);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(500).build();
    }

    /**
     * 日志翻页
     * @param sortProp
     * @param page
     * @param pageSize
     * @param sortOrder
     * @param uid
     * @param type
     * @return
     */
    @GetMapping("/logs")
    public ResponseEntity<PageResult> queryComment(@RequestParam(value = "sortProp",defaultValue = "created") String sortProp,
                                                   @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                   @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                                   @RequestParam(value = "sortOrder",defaultValue = "descending") String sortOrder,
                                                   @RequestParam("uid") String uid,
                                                   @RequestParam(value = "type",required = false) String type) {
        try {
            PageResult pageResult = this.manageService.querylogs(sortProp,page,pageSize,sortOrder,uid,type);
            if(pageResult.getItems() != null){
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 视频记录翻页
     * @param sortProp
     * @param page
     * @param pageSize
     * @param sortOrder
     * @param uid
     * @return
     */
    @GetMapping("/videos")
    public ResponseEntity<PageResult> queryVideosList(@RequestParam(value = "sortProp",defaultValue = "created") String sortProp,
                                                   @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                   @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                                   @RequestParam(value = "sortOrder",defaultValue = "descending") String sortOrder,
                                                   @RequestParam("uid") Integer uid) {
        try {
            PageResult pageResult = this.manageService.queryVideosList(sortProp,page,pageSize,sortOrder,uid);
            if(pageResult.getItems() != null){
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @GetMapping("/users/{userID}")
    public ResponseEntity<UserInfoVo> queryUserById(@PathVariable("userID") Long userId) {
        try {
            UserInfoVo userInfoVo = manageService.queryUserInfo(userId);
            if (ObjectUtil.isNotEmpty(userInfoVo)) {
                return ResponseEntity.ok(userInfoVo);
            }
        } catch (Exception e) {
            log.error("查询用户基本资料出错 userId= " + userId, e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @PostMapping("/users/freeze")
    public ResponseEntity<String> freeze(@RequestBody Map<String, String> param) {
        String userId = null;
        try {
            userId = param.get("userId");
            String freezingTime = param.get("freezingTime");
            String freezingRange = param.get("freezingRange");
            String reasonsForFreezing = param.get("reasonsForFreezing");
            String frozenRemarks = param.get("frozenRemarks");

            String result = manageService.freeze(userId, freezingTime, freezingRange, reasonsForFreezing, frozenRemarks);
            if (null != result) {
                String message = result.toString();
                return ResponseEntity.ok(message);
            }
        } catch (Exception e) {
            log.error("冻结用户出错 userId= " + userId, e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    @PostMapping("/users/unfreeze")
    public ResponseEntity<String> unfreeze(@RequestBody Map<String, String> param) {

        String userId = null;
        try {
            userId = param.get("userId");
            String reasonsForThawing = param.get("reasonsForThawing");
            Boolean flag = manageService.unfreeze(userId, reasonsForThawing);

            if (null != flag) {
                String message = flag.toString();
                return ResponseEntity.ok(message);
            }
        } catch (Exception e) {
            log.error("用户解冻失败 userId= "+userId,e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
